package com.witfortos.common.base

import android.util.Log
import com.witfortos.common.http.ResponseThrowable

/**
 *   @auther : tan
 *   time   : 2020-07-10
 */
abstract class BaseModel {

    /**
     * @param remoto 网络数据
     * @param save 当网络请求成功后，保存数据等操作
     */
    suspend fun <T> cacheNetCall(
            remoto: suspend () -> IBaseResponse<T>,
            save: suspend (T) -> Unit
    ): T {
        val net = remoto()
        Log.e("BaseModel", "请求结果$net");
        if (net.isSuccess()) {
            Log.e("BaseModel", "请求成功=$net");
            return net.data()!!.also { save(it) }
        }
        throw ResponseThrowable(net)
    }

    fun onCleared() {
    }
}